package com.tang.thepath;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tang.thepath.model.RequestModel;
import com.tang.thepath.service.BizService;
import com.tang.thepath.service.HttpSubscriber;
import com.tang.thepath.view.ImageUploader;

import java.util.ArrayList;


public class CustomInfoActivity extends BaseActivity {


    private EditText editCompanyName;
    private EditText editAddress;
    private EditText editContact;
    private EditText editPosition;
    private EditText editMobile;
    private Button btnSave;
    private ImageUploader imageUploader;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_info);

        editCompanyName = (EditText) findViewById(R.id.edit_company);
        editAddress = (EditText) findViewById(R.id.edit_address);
        editContact = (EditText) findViewById(R.id.edit_contact);
        editPosition = (EditText) findViewById(R.id.edit_position);
        editMobile = (EditText) findViewById(R.id.edit_mobile);
        btnSave = (Button) findViewById(R.id.btn_save);
        imageUploader = (ImageUploader) findViewById(R.id.img_uploader);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Const.NETWORK_MOD == 0){
                    toast("请检查您的网络设置");
                    return;
                }
                String companyName = editCompanyName.getText().toString();
                String address = editAddress.getText().toString();
                String contact = editContact.getText().toString();
                String position = editPosition.getText().toString();
                String mobile = editMobile.getText().toString();


                //验证信息
                if(companyName.isEmpty()){
                    toast("公司名称不能为空");
                    return;
                }

                final RequestModel.StoreCustomRequest request = new RequestModel.StoreCustomRequest();
                request.outsignId = 0;
                request.companyName = companyName;
                request.address = address;
                request.contact = contact;
                request.position = position;
                request.mobile = mobile;


                //上传图片
                if(imageUploader.imageCount() > 0){
                    showLoading("正在上传图片");
                    imageUploader.uploadImages().subscribe(new HttpSubscriber<RequestModel.UploadResponse>() {
                        @Override
                        protected void onSuccess(RequestModel.UploadResponse uploadResponse) {
                            dismisLoading();
                            request.images = uploadResponse.pathList;
                            uploadCustomInfo(request);
                        }
                    });
                }else{
                    request.images = new ArrayList<>();
                    uploadCustomInfo(request);
                }
            }
        });
    }


    protected void uploadCustomInfo(final RequestModel.StoreCustomRequest request){
        showLoading("数据提交中");
        BizService.storeCustom(request).subscribe(new HttpSubscriber<RequestModel.StoreCustomResponse>() {
            @Override
            protected void onSuccess(RequestModel.StoreCustomResponse storeCustomResponse) {
                String message = "存储成功:"+storeCustomResponse.id;
                Log.i(getTag(),message);
                toast(message);
                dismisLoading();
                finish();
            }
        });
    }

    @Override
    public ImageUploader getImageUploader() {
        return imageUploader;
    }
}
